using System;
using System.Collections.Generic;
using System.Text;

namespace SharpSoft.Html
{
    using AngleSharp.Dom;
    using Newtonsoft.Json;
    using System.Threading.Tasks;

    /// <summary>
    /// 抓取网站
    /// </summary>
    public class HtmlFetchingSite
    {
        /// <summary>
        /// 站点路径
        /// </summary>
        public string url { get; set; }
        /// <summary>
        /// 站点名称
        /// </summary>
        public string name { get; set; }
        /// <summary>
        /// 站点描述
        /// </summary>
        public string des { get; set; }
        /// <summary>
        /// 网站页面描述
        /// </summary>
        public HtmlFetchingPage[] pages { get; set; }

        public static HtmlFetchingSite Load(string filename)
        {
            return JsonHelper.DeserializeFromFile<HtmlFetchingSite>(filename);
        }
        public  HtmlFetchingPage GetPage(string pagename)
        {
            foreach (var item in pages)
            {
                if (item.name.EqualsIgnoreCase(pagename))
                {
                    return item;
                }
            }
            throw new Exception($"未发现指定名称的页面：[{pagename}]。");
        }

        private HtmlFetchingPage getLastPage(HtmlFetchingPage page)
        {
            var index = pages.IndexOf(page);
            if (index == -1)
            {
                throw new Exception("找不到指定的页面。");
            }
            if (index <= 0)
            {
                return null;
            }
            return pages[index - 1];
        }
        private HtmlFetchingPage getNextPage(HtmlFetchingPage page)
        {
            var index = pages.IndexOf(page);
            if (index == -1)
            {
                throw new Exception("找不到指定的页面。");
            }
            if (index >= pages.Length - 1)
            {
                return null;
            }
            return pages[index + 1];
        }
        
    }
}
